Submodule compress.zlib | Tarantool
Документация на русском языке
поддерживается сообществом

Submodule compress.zlib

The compress.zlib submodule provides the ability to compress and decompress data using the zlib algorithm. You can use the zlib compressor as follows:

  1. Create a compressor instance using the compress.zlib.new() function:

    local zlib_compressor = require('compress.zlib').new()
    -- or --
    local zlib_compressor = require('compress').zlib.new()
    

    Optionally, you can pass compression options (zlib_opts) specific for zlib:

    local zlib_compressor = require('compress.zlib').new({
        level = 5,
        mem_level = 5,
        strategy = 'filtered'
    })
    
  2. To compress the specified data, use the compress() method:

    compressed_data = zlib_compressor:compress('Hello world!')
    
  3. To decompress data, call decompress():

    decompressed_data = zlib_compressor:decompress(compressed_data)
    

Functions  
compress.zlib.new() Create a zlib compressor instance.
Objects  
zlib_compressor A zlib compressor instance.
zlib_opts Configuration options of the zlib compressor.

compress.zlib.new([zlib_opts])

Create a zlib compressor instance.

Параметры:
Return:

a new zlib compressor instance (see zlib_compressor)

Rtype:

userdata

Example

local zlib_compressor = require('compress.zlib').new({
    level = 5,
    mem_level = 5,
    strategy = 'filtered'
})

object zlib_compressor

A compressor instance that exposes the API for compressing and decompressing data using the zlib algorithm. To create the zlib compressor, call compress.zlib.new().

zlib_compressor:compress(data)

Compress the specified data.

Параметры:
  • data (string) – data to be compressed
Return:

compressed data

Rtype:

string

Example

compressed_data = zlib_compressor:compress('Hello world!')
zlib_compressor:decompress(data)

Decompress the specified data.

Параметры:
  • data (string) – data to be decompressed
Return:

decompressed data

Rtype:

string

Example

decompressed_data = zlib_compressor:decompress(compressed_data)

object zlib_opts

Configuration options of the zlib_compressor. These options can be passed to the compress.zlib.new() function.

Example

local zlib_compressor = require('compress.zlib').new({
    level = 5,
    mem_level = 5,
    strategy = 'filtered'
})
zlib_opts.level

Specifies the zlib compression level that enables you to adjust the compression ratio and speed. The lower level improves the compression speed at the cost of compression ratio.

Default: 6
Minimum: 0 (no compression)
Maximum: 9
zlib_opts.mem_level

Specifies how much memory is allocated for the zlib compressor. The larger value improves the compression speed and ratio.

Default: 8
Minimum: 1
Maximum: 9
zlib_opts.strategy

Specifies the compression strategy. The possible values:

  • default - for normal data.
  • huffman_only - forces Huffman encoding only (no string match). The fastest compression algorithm but not very effective in compression for most of the data.
  • filtered - for data produced by a filter or predictor. Filtered data consists mostly of small values with a somewhat random distribution. This compression algorithm is tuned to compress them better.
  • rle - limits match distances to one (run-length encoding). rle is designed to be almost as fast as huffman_only but gives better compression for PNG image data.
  • fixed - prevents the use of dynamic Huffman codes and provides a simpler decoder for special applications.
Нашли ответ на свой вопрос?
Обратная связь